package com.tuling.controller;

import com.tuling.domain.Fruit;
import com.tuling.service.FruitService;
import com.tuling.service.FruitServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "SearchServlet", value = "/searchServlet")
public class SearchServlet extends HttpServlet {
    private FruitService fruitService = new FruitServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        // 获取数据
        String method = request.getParameter("method");
        // 根据method做不同处理
        if (method.equals("search")) {
            // 搜索
            search(request, response);
        } else if (method.equals("money")) {
            // 按金额搜索
            money(request, response);
        } else if (method.equals("all")) {
            searchAll(request, response);
        }

    }

    private void money(HttpServletRequest request, HttpServletResponse response) {
        // 获取参数
        String keyword = request.getParameter("keyword");
        int start = 0;
        int end = 100;
        if ("a".equals(keyword)) {
            end = 100;
        }
        if ("b".equals(keyword)) {
            start = 100;
            end = 300;
        }
        if ("c".equals(keyword)) {
            start = 300;
            end = 999999;
        }
        List<Fruit> all = fruitService.getAll();
        List<Fruit> fruits = new ArrayList<>();
        for (Fruit fruit : all) {
            if (fruit.getUp()>=start && fruit.getUp()<=end) {
                fruits.add(fruit);
            }
        }
        request.setAttribute("selFruits", fruits);
        try {
            request.getRequestDispatcher("/search.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void search(HttpServletRequest request, HttpServletResponse response) {

        String keyword = request.getParameter("keyword");
        if (keyword == null || keyword.equals("")) {
            searchAll(request, response);
            return;
        }
        List<Fruit> byKey = fruitService.findByKey(keyword);
        request.setAttribute("selFruits", byKey);
        try {
            request.getRequestDispatcher("/search.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void searchAll(HttpServletRequest request, HttpServletResponse response) {
        List<Fruit> all = fruitService.getAll();
        request.setAttribute("selFruits", all);
        try {
            request.getRequestDispatcher("/search.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
